'''Coding style.

    Name classes as UpperCamelCase,
    Name functions and methods as lowercase_with_underscroes,
    Name variables as oneword.
'''
# -*- coding: UTF-8 -*-
from tkinter import *
import tkinter.filedialog as filedialog
import io
import re
import os

NoteS = Tk()

# 设置主窗口大小，位置在屏幕中心
# 注意：主窗体的 geometry 应在设置其他属性之前优先设置，否则主窗体的位置不能正确设置
# ===========================
screen_width = NoteS.winfo_screenwidth()
screen_height = NoteS.winfo_screenheight()
# print('部件屏幕宽度的像素数', screenwidth, screenheight)

NoteS_width = 600
NoteS_height = 500   # 这里的高度不包括标题栏的高度
NoteS_padx = int((screen_width-NoteS_width)/2)
NoteS_pady = int((screen_height-NoteS_height)/2)
NoteSgeometry = '{}x{}+{}+{}'.format(NoteS_width, NoteS_height, NoteS_padx, NoteS_pady)
NoteS.geometry(NoteSgeometry)
# print('主窗口大小和定位', NoteSgeometry, NoteS.winfo_geometry())
# python3.8, win10 系统下，主窗口的位置使用 geometry() 函数设置无效。
# ===========================

NoteS.title('NoteS')                # 主窗口标题
NoteS.iconbitmap(r'Icon\NoteS.ico') # 主窗口左上角图标



# head_tab frame
head_tab = Frame(NoteS, bg = '#ECECEC', bd=0)
head_tab.pack(fill = X, anchor = N)

# path_dispaly frame
path_dispaly = Frame(NoteS, bg = '#eee', )
path_dispaly.pack(anchor = W)

filedir = Label(path_dispaly)

# text_input frame
text_frame = Frame(NoteS, bg = '#fff')
text_frame.pack(fill = BOTH, expand = True)

text_input=Text(text_frame)

# 子菜单事件
def new_file():    
    filename = Label(head_tab, text = 'Untitled-1', bg = '#ECECEC',)
    filename.pack(side=LEFT)
    close = PhotoImage(file=r'Image\close.png')
    close_button = Label(head_tab, image = close)
    close_button.pack(side=LEFT)
    
    text_input.config(bd = 0, bg='#fff', fg='#000', font=('Consolas', 14))
    text_input.pack(fill = BOTH, expand = True)

def save_file():
    file = open(r'NoteBook\new.txt','w+')
    text = text_input.get('0.0', 'end')
    file.write(text)
    file.close()
    NoteS.title('new.txt-NoteS')
    
    pwd = os.getcwd()
    filedir.config(text = pwd, bg = '#fff')
    filedir.pack()

def open_file():
    file_name = filedialog.askopenfilename(filetypes=[('Python', '.py'), ('Json', '.json'), ('Text', '.txt'), ('Markdown', '.md')])
    filedir.config(text = file_name)
    filedir.pack()

def func():
    pass

def Exit():
    NoteS.destroy()

# 顶层菜单
topmenu = Menu(NoteS)

# 子菜单配置
menuconfig = {
    'File': {
        'New': new_file,
        'Separator0': 0,
        'Save': save_file,
        'Save as': func,
        'Separator1': 1,
        'Open file': open_file,
        'Open folder': func,
        'Separator2': 2,
        'Exit': Exit
    },
    'Edit':{
        'Undo': func,
        'Redo': func,
        'Separator0': 0,
        'Copy': func,
        'Cut': func,
        'Paste': func,
        'Separator0': 1,
        'Find': func,
        'Replace': func,
    },
    'Settings':{
        'Preferences': func,
        'Keyboard Shortcuts': func,
        'Separator0': 0,
        'Extensions': func,
    },
    'Help':{
        'Relase Notes': func,
        'Keyboard Shortcuts Reference': func,
        'Separator0': 0,
        'About': func
    }
}

# 创建子菜单 accelerator = "Ctrl+N"
for key in list(menuconfig.keys()):
    submenu = Menu(topmenu, tearoff=0, bg='#fff',)
    for subkey in list(menuconfig[key].keys()):     
        if re.match('Separator', subkey):
            submenu.add_separator()
            continue
        submenu.add_command(label=subkey, command=menuconfig[key][subkey])
    topmenu.add_cascade(label=key, menu=submenu)

# 配置菜单
NoteS.config(menu=topmenu)

NoteS.mainloop()